package cn.chenc.framework.security.model;

import cn.chenc.framework.core.model.LoginUserDetails;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

/**
 * 　@description: 实现SpringSecurity的UserDetails
 * 　@author secret
 * 　@date 2021/4/1 14:40
 *
 */
public class SecurityLoginUser implements SecurityAbstractLoginUser, Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 角色列表
     */
    private Set<String> roles;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    /**
     * 用户信息
     */
    private LoginUserDetails user;

    private Set<GrantedAuthority> authorities;

    @Override
    public String getToken() {
        return token;
    }

    @Override
    public void setToken(String token) {
        this.token = token;
    }

    public SecurityLoginUser() {

    }

    public SecurityLoginUser(LoginUserDetails user, Set<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }


    public SecurityLoginUser(LoginUserDetails user, Set<String> permissions, Set<GrantedAuthority> authorities) {
        this.user = user;
        this.permissions = permissions;
        this.authorities = authorities;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isEnabled()
    {
        return true;
    }

    @Override
    public Long getLoginTime()
    {
        return loginTime;
    }

    @Override
    public void setLoginTime(Long loginTime)
    {
        this.loginTime = loginTime;
    }

    @Override
    public String getIpaddr()
    {
        return ipaddr;
    }

    @Override
    public void setIpaddr(String ipaddr)
    {
        this.ipaddr = ipaddr;
    }

    @Override
    public String getLoginLocation()
    {
        return loginLocation;
    }

    @Override
    public void setLoginLocation(String loginLocation)
    {
        this.loginLocation = loginLocation;
    }

    @Override
    public String getBrowser()
    {
        return browser;
    }

    @Override
    public void setBrowser(String browser)
    {
        this.browser = browser;
    }

    @Override
    public String getOs()
    {
        return os;
    }

    @Override
    public void setOs(String os)
    {
        this.os = os;
    }

    @Override
    public Long getExpireTime()
    {
        return expireTime;
    }

    @Override
    public void setExpireTime(Long expireTime)
    {
        this.expireTime = expireTime;
    }

    @Override
    public Set<String> getRoles() {
        return roles;
    }

    @Override
    public void setRoles(Set<String> roles) {
        this.roles = roles;
    }

    @Override
    public Set<String> getPermissions()
    {
        return permissions;
    }

    @Override
    public void setPermissions(Set<String> permissions)
    {
        this.permissions = permissions;
    }

    @Override
    public LoginUserDetails getUser() {
        return user;
    }

    @Override
    public void setUser(LoginUserDetails user) {
        this.user = user;
    }


    public void setAuthorities(Set<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities()
    {
        return this.authorities;
    }
}
